#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Author : raoxiaoyang
# @File : temperatureSensor.py
# @Software: PyCharm
# @Function: 获取温度传感器读出的数据，单位为摄氏度
# @Usage: a = SensorTem()
#         a.printTemperature()

from pi.controller.sensor.temperature import PCF8591 as ADC
import RPi.GPIO as GPIO
import math


class SensorTem:
    def __init__(self):
        '''
        初始化
        '''
        self.makerobo_DO = 17                          # 温度传感器Do管脚
        GPIO.setmode(GPIO.BCM)                         # 管脚映射，采用BCM编码
        self.makerobo_setup()

    def makerobo_setup(self):
        '''
        配置信息
        '''
        ADC.setup(0x48)                                # 设置PCF8591模块地址
        GPIO.setup(self.makerobo_DO, GPIO.IN)          # 温度传感器DO端口设置为输入模式

    def printTemperature(self):
        '''
        打印获取的值
        '''
        makerobo_analogVal = ADC.read(0)                       # 读取AIN0上的模拟值
        makerobo_Vr = 5 * float(makerobo_analogVal) / 255      # 转换到5V范围
        makerobo_Rt = 10000 * makerobo_Vr / (5 - makerobo_Vr)
        makerobo_temp = 1/(((math.log(makerobo_Rt / 10000)) / 3950) + (1 / (273.15+25)))  #华氏温度
        makerobo_temp = makerobo_temp - 273.15                 # 转换成摄氏温度
        return makerobo_temp
